<template>
  <div>
    <el-form ref="loginForm" :model="form" :rules="rules" label-width="80px" class="login-box">
      <h3 class="login-title">欢迎登录</h3>
      <el-form-item label="提款机" prop="cashDispenserId">
        <el-select  v-model="form.cashDispenserId" placeholder="请选择">
          <el-option
            v-for="item in Cashdispenser"
            :key="item.cashDispenserId"
            :label="item.cashDispenserId"
            :value="item.cashDispenserId"
            :disabled="item.status > 0"
          >
            <span style="float: left">{{ item.cashDispenserId }}号提款机</span>
            <span v-if="item.status== 0" style="float: right; color: #8492a6; font-size: 13px">状态:正常</span>
            <span v-if="item.status > 0"  style="float: right; color: #8492a6; font-size: 13px">状态:异常</span>
          </el-option>
        </el-select><p v-model="stats" v-text="stats" class="stat"></p>
      </el-form-item>
      <el-form-item label="账号" prop="bankcardNumber">
        <el-input type="text" placeholder="请输入账号" @blur="checkBankcardNumber()" v-model="form.bankcardNumber"/>
      </el-form-item>
      <el-form-item label="密码" prop="bankcardPassword">
        <el-input type="password" placeholder="请输入密码" v-model="form.bankcardPassword"/>

      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('loginForm')">登录</el-button>
        <el-button @click="resetForm('loginForm')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-dialog
      title="温馨提示"
      :visible.sync="dialogVisible"
      width="30%">
      <span><p v-model="msg" v-text="msg" class="color:red"></p></span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    name: "Login",
    data() {
      return {
        stats:null,
        msg: null,
        radio:"",
        Cashdispenser: [{
          status: '',
          cashDispenserId: '',
        }],
        form: {
          bankcardNumber: '',
          bankcardPassword: '',
          cashDispenserId:'',
        },

        // 表单验证，需要在 el-form-item 元素中增加 prop 属性
        rules: {
          bankcardNumber: [
            {required: true, message: '卡号不可为空', trigger: 'blur'}
          ],
          bankcardPassword: [
            {required: true, message: '密码不可为空', trigger: 'blur'}
          ],
          cashDispenserId:[
            {required: true, message:'请选择提款机',trigger:'blur'}
          ]
        },
        // 对话框显示和隐藏
        dialogVisible: false
      }
    },

    methods: {
      onSubmit(formName) {
        // 为表单绑定验证功能
        this.$refs[formName].validate((valid) => {
          if (this.stats != null) {
            this.msg = this.stats;
            this.dialogVisible = true;
            return false;
          }
          if (!valid) {
            return false;
          }
          this.$axios.post(this.global.serverPath +'/BankCardLogin/', this.$qs.stringify(this.form)).then(response => {
            if (response.data != true) { // 不为true 说明登录失败
              this.msg = response.data; // 提示用户为什么不能登录
              this.dialogVisible = true;// 打开弹窗
              this.checkBankcardNumber();
              return;
            }
            if (response.data == true) {
              sessionStorage.setItem("bankcardNumber",this.form.bankcardNumber);
              this.$router.push("/index");
            }
            // 成功跳转主页面
          });
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      checkBankcardNumber(){
        this.stats="";
        this.$axios.post(this.global.serverPath +'/BankCardCheck/' , this.$qs.stringify(this.form)).then(response => {
          if (response.data != true) {
            this.stats = response.data;
            this.rules.bankcardNumber.required=true;
            return
          }
          else {
            this.stats = null;
            return
          }
        }).catch(err => {
          console.log(err+"--------")
        });}
    },
    mounted() {
        this.$axios.post(this.global.serverPath +'/ListCashDispenser/').then(response => {
        this.Cashdispenser = response.data
      }).catch(err => {
      });
    }
  }
</script>

<style lang="scss" scoped>
  .login-box {
    border: 1px solid #DCDFE6;
    width: 350px;
    margin: 180px auto;
    padding: 35px 35px 15px 35px;
    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    box-shadow: 0 0 25px #909399;
  }

  .login-title {
    text-align: center;
    margin: 0 auto 40px auto;
    color: #303133;
  }
  .stat{
    color: #F56C6C;
    font-size: 12px;
    line-height: 0;
    position: absolute;
    top: 100%;
    left: 0;
  }

</style>
